//
//  NSDate+Interval.m
//  事件处理
//
//  Created by 陈博文 on 15/12/26.
//  Copyright © 2015年 陈博文. All rights reserved.
//

#import "NSDate+Interval.h"

#import "NSCalendar+Init.h"

@implementation NSDate (Interval)


- (BOOL)bs_isToday
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";//只是使用年月日,不使用时分秒
    
    NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 目前事件
    NSString *selfString = [fmt stringFromDate:self]; // 调用这个方法传进来的时间
    
    return [nowString isEqualToString:selfString];
}


- (BOOL)bs_isThisYear
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy";
    
    NSString *nowYear = [fmt stringFromDate:[NSDate date]];
    NSString *selfYear = [fmt stringFromDate:self];
    
    return [nowYear isEqualToString:selfYear];
    }


- (BOOL)bs_isYesterday
{
        
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    
    // 生成只有年月日的字符串
    NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 现在的时间
    NSString *selfString = [fmt stringFromDate:self]; // 传进来的时间
    
    // 生成只有年月日的日期
    NSDate *nowDate = [fmt dateFromString:nowString];
    NSDate *selfDate = [fmt dateFromString:selfString];
    
    // 比较nowDate和selfDate的差值
    NSCalendar *calendar = [NSCalendar bs_calendar];
    
    //使用NSDateComponents进行时间比较
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
    //返回的三个值是一个 BOOL,判断最后的值是否为0(或者1)
    return cmps.year == 0
    && cmps.month == 0
    && cmps.day == 1;
}

- (BOOL)bs_isTomorrow
{    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    
    // 生成只有年月日的字符串
    NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20151201
    NSString *selfString = [fmt stringFromDate:self]; // 20151130
    
    // 生成只有年月日的日期
    NSDate *nowDate = [fmt dateFromString:nowString];
    NSDate *selfDate = [fmt dateFromString:selfString];
    
    // 比较nowDate和selfDate的差值
    NSCalendar *calendar = [NSCalendar bs_calendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
    
    return cmps.year == 0
    && cmps.month == 0
    && cmps.day == -1;
}


@end
